<?php
/*
This sends a little bit of JSON to say if the user is logged in. 
*/

require_once( 'core.php' );
require_once( 'current_user_api.php' );
require_once( 'string_api.php' );
require_once( 'file_api.php' );
require_once( 'bug_api.php' );
require_once( 'custom_field_api.php' );
require_once( 'collapse_api.php' );
require_once( 'relationship_api.php' );
require_once( 'project_api.php' );

auth_ensure_user_authenticated( );
//helper_begin_long_process( );

$t_plugin_path = config_get( 'plugin_path' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'ProjectsMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'TableMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'JSONstore.php' );

$authHashPrefix = plugin_config_get( 'authHashPrefix' );
$refineHumanURI = plugin_config_get( 'refineBaseURI' ); //for human linking
$refineURI = plugin_config_get( 'refineLocalURI' );
if(!$refineURI){
	$refineURI = plugin_config_get( 'refineBaseURI' );
}

# extracts the user information for the currently logged in user
# and prefixes it with u_
$f_user_id = gpc_get_int( 'id', auth_get_current_user_id() );
$row = user_get_row( $f_user_id );

extract( $row, EXTR_PREFIX_ALL, 'u' );
$u_email = user_get_email( $u_id );
$u_realname = user_get_realname( $u_id );



//Needed for making an issue
$t_project_id = helper_get_current_project();
	

$projectsMetadataObj = new ProjectsMetadata;
$projectsMetadataObj->authHashPrefix = $authHashPrefix;
$projectsMetadataObj->refineURI = $refineURI;
$projectsMetadataObj->refineHumanURI = $refineHumanURI;
$projectsMetadataObj->fallBack_issueProject_id = $t_project_id;
$projectsMetadataObj->fallBack_issueUserID = auth_get_current_user_id();
$projectsMetadataObj->fallBack_issueProfile_id = gpc_get_int( 'profile_id', 0 );

$projectsMetadataObj->get_gr_projects();
$projectsMetadataObj->get_only_projectsdata();
$projectsMetadataObj->update_current_projects();

$currentProjectDatasets = false;
$allDatasets = $projectsMetadataObj->projectsMetadata;
if(is_array($allDatasets)){
	if(array_key_exists($t_project_id, $allDatasets)){
		$currentProjectDatasets = $allDatasets[$t_project_id];
	}
	elseif($t_project_id == 0){
		$currentProjectDatasets = array();
		foreach($allDatasets as $issuesProjID => $actProjectArray){
			foreach($actProjectArray as $GRprojectID => $projectDataset){
				$currentProjectDatasets[$GRprojectID] = $projectDataset;
			}
		}
	}
}

html_page_top( "Recipes for Linking Data" );

?>

<div class="data-doc-page-body">
	
	<!-- page heading information -->
	<div class="data-doc-page-heading">
		<div class="data-doc-page-heading-row">
			<div class="data-doc-page-heading-title">
				<h2>How to Link Data</h2>
			</div>
			<div class="data-doc-page-heading-dset">
				<div class="data-doc-page-heading-dset-tab">
					<div class="data-doc-page-heading-dset-row">
						<div class="data-doc-page-heading-dset-title">
							<?php
							/*
							if($t_project_id == 0){
								echo echo "<h3>All datasets, for all projects</h3>";
							}
							else{
								
							}
							*/
							$actProjectName =  project_get_name($t_project_id);
							echo "<h3>Datasets assoiated with &ldquo;".$actProjectName."&rdquo;</h3>"; 
							
							?>
						</div>
						<div class="data-doc-page-heading-dset-des">
						The issue tracker currently monitors
						
						<?php
						
							$actDataSetCount = 0;
							echo " <span id=\"dataset-row-count\">";
							if(is_array($currentProjectDatasets)){
								$actDataSetCount = count($currentProjectDatasets);
								echo $actDataSetCount;
							}
							else{
								echo "0";
							}
							echo "</span>";
						
							if($t_project_id == 0){
								echo " datasets in all of the issue-tracker's projects. ";
							}
							else{
								echo " datasets in the current project. ";
							}
							
							if($actDataSetCount>0){
								//echo "Click on one of the tracked datasets linked below to view summary status information, and begin to resolve issues an add needed documentation. ";
							}
							
							if(is_array($projectsMetadataObj->unTrackedProjects)){
								echo "<br/><br/>";
								echo "Currently, Google Refine has <span id=\"grNotTrackCount\">".count($projectsMetadataObj->curGoogleRefineProjects)."</span> datasets not yet tracked for issues or data documentation.";
							}
							
						?>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<!-- Main content for this dataset tracking page -->	
	<div class="data-doc-page-primary">
		<h3>Tips, Recipes, and How-To's</h3>
		<p>Google Refine is a powerful tool for reviewing and cleaning data. Linking datasets to shared vocabularies and concepts
		published on the Web can help make datasets even more useful to more people. Below we illustrate a few examples for how
		to link data to concepts that may be relevant to archaeology.</p>
		<p>The tips below help guide data editors in <em>entity reconciliation</em> (the determination of a Web identifier for a
		shared concept). By referencing shared concepts, datasets become more intelligible and better contextualized.</p> 
		<table>
			<thead>
				<tr>
					<th>Vocabulary</th>
					<th>Appliations</th>
					<th>Google Refine Instructions</th>
				</tr>
			</thead>
			<tbody>
				<tr class="rec-row-a">
					<td class="rec-col-repository">Encyclopedia of Life (EOL)
					<br/>
					<a href="http://eol.org">http://eol.org</a>
					</td>
					<td class="rec-col-exp">The EOL maintains a database of biological taxa that
					reconciles several taxonomic systems. Each taxonomic unit has
					it's own Web page, identified with a stable URI. Reference to
					these URIs can add precision to taxonomic identification of
					contributed datasets.
					</td>
					<td class="rec-col-howto">
						The EOL has a simple Web-service useful looking up EOL identifiers
					from taxonomic terms. Latin taxonomic terms tend to work better because
					they have less ambiguity than informal common names for taxa.
						<h4>Procedure (in Google Refine)</h4>
						<ol>
							<li class="rec-item">Click on the column with biological taxa terms to select it. Create a column for the EOL identifiers of taxa by <em>Add Column By Fetching URLs... </em>
							</li>
							<li class="rec-item">In the expression field, write:<br/>
							<div class="rec-code">
								&quot;http://eol.org/api/search/&quot; + escape(value, "url") + &quot;.json&quot;
							</div>
							The adove expression will get a list of possible EOL identifiers, ranked from most
							to least likely.
							</li>
							<li class="rec-item">Completing the step above will populate this new column with JSON data from the EOL.
							You'll need to process these results further with the <em>Edit Cells > Transform...</em>
							command.
							</li>
							<li class="rec-item">In the expression field, write:<br/>
								<div class="rec-code">
								&quot;http://eol.org/pages/&quot; + value.parseJson()[&quot;results&quot;][0][&quot;id&quot;]
								</div>
								The above expression generates a link to an EOL Web page representing
								the mostly likely match to the taxonomic term in your dataset.
							</li>
							<li class="rec-item"><strong>Check the results!</strong> This is a <em>semi-automated</em> approach. You'll need
							to check to make sure the results make sense and reference appropriate EOL pages.
							</li>
						</ol>
					</td>
				</tr>
			</tbody>
		</table>
	</div>
</div>

	

<?php
html_page_bottom();
?>
